<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:details:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:details:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:details:export']"
        >打印</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="detailsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="索引" align="center" prop="id" />
      <el-table-column label="耗材编号" align="center" prop="hcode" />
      <el-table-column label="耗材名称" align="center" prop="hname" />
      <el-table-column label="采购数量" align="center" prop="num" />
      <el-table-column label="供应商" align="center" prop="sup" />
      <el-table-column label="单价" align="center" prop="hprice" />
      <el-table-column label="采购单编号" align="center" prop="hpurchase" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:details:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:details:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-row :gutter="10" class="mb8" type="flex" justify="center" align="middle">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon=""
          size="medium"
          @click="handleSave"
          v-hasPermi="['system:details:save']"
        >保存</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon=""
          size="medium"
          @click="backHistory"
          v-hasPermi="['system:details:save']"
        >返回</el-button>
      </el-col>
    </el-row>
    <!-- 添加或修改采购单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="耗材编号" prop="hcode">
          <el-input v-model="form.hcode" placeholder="请输入耗材编号" />
        </el-form-item>
        <el-form-item label="耗材名称" prop="hname">
          <el-input v-model="form.hname" placeholder="请输入耗材名称" />
        </el-form-item>
        <el-form-item label="采购数量" prop="num">
          <el-input v-model="form.num" placeholder="请输入采购数量" />
        </el-form-item>
        <el-form-item label="供应商" prop="sup">
          <el-input v-model="form.sup" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="单价" prop="hprice">
          <el-input v-model="form.hprice" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="采购单" prop="hpurchase">
          <el-input v-model="cCode"placeholder="请输入采购单编号" disabled />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {list,listDetails, getDetails, delDetails, addDetails, updateDetails } from "@/api/system/details";

export default {
  name: "Details",
  data() {
    return {
      cCode:null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 医院耗材管理系统表格数据
      detailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hcode: null,
        hname: null,
        num: null,
        sup: null,
        hprice: null,
        hpurchase:null,
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    console.log("details:"+this.$route.query.code);
    this.cCode=this.$route.query.code;
    this.queryParams.hpurchase=this.cCode;
    this.getList();
  },
  methods: {
    /** 查询采购单列表 */
    getList() {
      this.loading = true;
      list(this.queryParams).then(response => {
        this.detailsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        hcode: null,
        hname: null,
        num: null,
        sup: null,
        hprice: null,
        hpurchase: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购条目";
    },
    /** 返回按钮操作 */
    backHistory() {
      this.$router.go(-1);//返回上一层
    },
    /** 保存操作 */
    handleSave() {
      alert("保存成功!")
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDetails(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.hpurchase=this.cCode;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDetails(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDetails(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function() {
        return delDetails(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/details/export', {
        ...this.queryParams
      }, `details_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
